using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace Genetic_Tetris
{
    class PieceFactory
    {
        Random rand;

        /// <summary>
        /// Reinitializes the Randomizer with a new seed
        /// </summary>
        public int RandomSeed
        {
            set { rand = new Random(value); }
        }

        public enum PieceType
        {
            I = 0,
            J = 1,
            L = 2,
            O = 3,
            S = 4,
            T = 5,
            Z = 6

        }


        public PieceFactory()
        {
        }

        /// <summary>
        /// Returns a random piece
        /// </summary>
        /// <returns></returns>
        public Piece MakePiece()
        {
            int pieceNum = rand.Next(7);
            return MakePiece((PieceType)pieceNum);
        }


        /// <summary>
        /// Returns a piece of the specified type
        /// </summary>
        /// <param name="typeIn"></param>
        /// <returns></returns>
        public Piece MakePiece(PieceType typeIn)
        {
            Piece myPiece = new Piece();
            if (typeIn == PieceType.I)
            {
                /*
                 1023
                 */
                myPiece.Blocks[0] = new Point(0, 0);
                myPiece.Blocks[1] = new Point(-1, 0);
                myPiece.Blocks[2] = new Point(1, 0);
                myPiece.Blocks[3] = new Point(2, 0);
                

            }
            else if (typeIn == PieceType.J)
            {
                /*
                 102
                   3
                 */
                myPiece.Blocks[0] = new Point(0, 0);
                myPiece.Blocks[1] = new Point(-1, 0);
                myPiece.Blocks[2] = new Point(1, 0);
                myPiece.Blocks[3] = new Point(1, 1);

            }
            else if (typeIn == PieceType.L)
            {
                /*
                 103
                 2
                 */
                myPiece.Blocks[0] = new Point(0, 0);
                myPiece.Blocks[1] = new Point(-1, 0);
                myPiece.Blocks[2] = new Point(-1, 1);
                myPiece.Blocks[3] = new Point(1, 0);

            }
            else if (typeIn == PieceType.O)
            {
                /*
                 01
                 32
                 */
                myPiece.Blocks[0] = new Point(0, 0);
                myPiece.Blocks[1] = new Point(1, 0);
                myPiece.Blocks[2] = new Point(1, 1);
                myPiece.Blocks[3] = new Point(0, 1);
            }
            else if (typeIn == PieceType.S)
            {
                /*
                 01
                23
                 */
                myPiece.Blocks[0] = new Point(0, 0);
                myPiece.Blocks[1] = new Point(1, 0);
                myPiece.Blocks[2] = new Point(-1, 1);
                myPiece.Blocks[3] = new Point(0, 1);
            }
            else if (typeIn == PieceType.T)
            {
                /*
                 102
                  3
                 */
                myPiece.Blocks[0] = new Point(0, 0);
                myPiece.Blocks[1] = new Point(-1, 0);
                myPiece.Blocks[2] = new Point(1, 0);
                myPiece.Blocks[3] = new Point(0, 1);
            }
            else if (typeIn == PieceType.Z)
            {
                /*
                 10
                  23
                 */
                myPiece.Blocks[0] = new Point(0, 0);
                myPiece.Blocks[1] = new Point(-1, 0);
                myPiece.Blocks[2] = new Point(0, 1);
                myPiece.Blocks[3] = new Point(1, 1);
            }
            else
            {
                throw new Exception("Unknown piece type!!");
            }

            myPiece.Type = (Piece.PieceType)typeIn;
            return myPiece;

        }


    }
}
